function formatTime(duration) {
  let minutes = Math.floor(duration / 60000) // 计算分钟数
  let seconds = Math.floor(duration % 60000 / 1000) // 计算秒数
  return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}` // 返回格式化后的字符串
}

function formatTimeSecond(duration) {
  const minutes = Math.floor(duration / 60);
  const seconds = Math.floor(duration % 60);
  return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}

function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}
// function formathm(timeString) {
//   const [minutesString, secondsString] = timeString.split(':');
//   const minutes = parseInt(minutesString, 10) || 0;
//   const seconds = parseFloat(secondsString) || 0;
//   return (minutes * 60 + seconds) * 1000;
// }


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function validLogin(url) {
  if (url === '/openapi/music/basic/oauth2/login/anonymous') {
    return true
  }
  const accessToken = wx.getStorageSync('accessToken');
  if (!accessToken) {
    wx.redirectTo({
      url: '/pages/login/login',
    })
    return false
  }
  return true
}
/************************歌曲操作 ********************/


var innerAudioContext = wx.createInnerAudioContext();
/**
 * 获取当前播放的时间
 */
function getinnerAudioContext() {
  return innerAudioContext
}
// var app = getApp()
/**
 * 播放
 * @param {*}
 */
function songPlayByUrl() {

  innerAudioContext.src = getApp().globalData.songPlayUrl;
  innerAudioContext.play();
  getApp().globalData.ifStop = false;

  innerAudioContext.onEnded(() => {
    console.log("准备自动播放下一首")
    this.nextSong();
  })
}

/**
 * 歌曲暂停
 */
function songPause() {
  var app = getApp()
  innerAudioContext.pause();
  app.globalData.ifStop = true;
}
/**
 * 歌曲停止
 */
// function songStop() {
//   var app=getApp()
//   innerAudioContext.stop();
//   app.globalData.ifStop=true
// }
/**
 * 下一首歌曲
 */
function nextSong() {
  var app = getApp();
  console.log(app.globalData.correctSongList);
  var songlist = app.globalData.correctSongList;
  var index = app.globalData.index;
  var http = require("../http/http")

  //切换下一首
  if (index < songlist.length - 1) {
    index = index + 1
  } else {
    index = 0
  }
  app.globalData.index = index;

  var id = songlist[index].id;
  app.globalData.correctSongId = id, //ID
    app.globalData.ifStop = false,

    http.getSongInformation(id).then(res => {
      //当前播放歌曲信息:
      app.globalData.correctSongId = id, //ID
        app.globalData.correctSongName = res.name, //歌曲名
        app.globalData.correctSongArtists = res.artists, //歌手s
        app.globalData.correctSongArtist = res.artistName, //歌手
        app.globalData.correctSongAlbum = res.albumName, //专辑
        app.globalData.songImg = res.coverImgUrl, //歌曲图标
        app.globalData.songPlayUrl = res.playUrl, // 歌曲播放URL
        app.globalData.songDuration = res.duration //歌曲时长
      //获取评论
      http.getSongCommonts(id).then(res => {
        getApp().globalData.songComments = res.records
      });

      console.log("赋值给全局变量完毕，开始播放")
      this.songPlayByUrl();


    });



}

/**
 * 上一首歌曲
 */
function lastSong() {
  var app = getApp();
  console.log(app.globalData.correctSongList);
  var songlist = app.globalData.correctSongList;
  var index = app.globalData.index;
  var http = require("../http/http")

  //切换上一首
  if (index == 0) {
    index = songlist.length - 1
  } else {
    index = index - 1
  }
  app.globalData.index = index;

  var id = songlist[index].id;
  app.globalData.correctSongId = id, //ID

    http.getSongInformation(id).then(res => {
      //当前播放歌曲信息:
      app.globalData.correctSongId = id; //ID
      app.globalData.correctSongName = res.name; //歌曲名
      app.globalData.correctSongArtists = res.artists, //歌手s
        app.globalData.correctSongArtist = res.artistName, //歌手
        app.globalData.correctSongAlbum = res.albumName, //专辑
        app.globalData.songImg = res.coverImgUrl, //歌曲图标
        app.globalData.songPlayUrl = res.playUrl // 歌曲播放URL
      //获取评论
      http.getSongCommonts(id).then(res => {
        getApp().globalData.songComments = res.records
      });

      console.log("赋值给全局变量完毕，开始播放")
      this.songPlayByUrl();


    });



}







module.exports = {
  formatTime,
  formatTimeTwo,
  validLogin,
  songPlayByUrl,
  songPause,
  nextSong,
  lastSong,
  getinnerAudioContext,
  formatTimeSecond
}